//
//  MYSearchResultTableViewCell.m
//  HeXun-zixuan
//
//  Created by 范名扬 on 16/3/18.
//  Copyright © 2016年 com.hztc. All rights reserved.
//

#import "MYSearchResultTableViewCell.h"

@interface MYSearchResultTableViewCell ()
@property (nonatomic, strong)UIImageView        *pImageView;
@property (nonatomic, strong)UILabel            *pStockName;
@property (nonatomic, strong)UILabel            *pStockCode;
@property (nonatomic, strong)UIButton           *pAddZiXuanBtn;
@end

@implementation MYSearchResultTableViewCell
-(UIImageView *)pImageView
{
    if (!_pImageView) {
        _pImageView = [[UIImageView alloc] init];
    }
    return _pImageView;
}
-(UILabel *)pStockName
{
    if (!_pStockName) {
        _pStockName = [[UILabel alloc] init];
    }
    return _pStockName;
}
-(UILabel *)pStockCode
{
    if (!_pStockCode) {
        _pStockCode = [[UILabel alloc] init];
    }
    return _pStockCode;
}
-(UIButton *)pAddZiXuanBtn
{
    if (!_pAddZiXuanBtn) {
        _pAddZiXuanBtn = [[UIButton alloc] init];
    }
    return _pAddZiXuanBtn;
}

+ (MYSearchResultTableViewCell *)searchResultCellTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath cellModel:(MYSearchResultModel *)cellModel cellHight:(CGFloat)cellHight
{
    return [self searchResultCellTableView:tableView indexPath:indexPath cellModel:cellModel cellHight:cellHight cellW:0];
}

+ (MYSearchResultTableViewCell *)searchResultCellTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath cellModel:(MYSearchResultModel *)cellModel cellHight:(CGFloat)cellHight cellW:(CGFloat)cellW
{
    return [[self alloc] initWithTableView:tableView indexPath:indexPath cellModel:cellModel cellHight:cellHight cellW:(CGFloat)cellW];
}

- (MYSearchResultTableViewCell *)initWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath cellModel:(MYSearchResultModel *)cellModel cellHight:(CGFloat)cellHight cellW:(CGFloat)cellW
{
    static NSString *ID = @"searchResultCell";
    MYSearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[MYSearchResultTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell initView];
    }
    [cell initView:cellHight cellModel:cellModel indexPath:indexPath cellW:(CGFloat)cellW];
    return cell;
}

- (void)initView
{
    [self.contentView addSubview:self.pImageView];
    [self.contentView addSubview:self.pStockName];
    [self.contentView addSubview:self.pStockCode];
    [self.contentView addSubview:self.pAddZiXuanBtn];
    
    _pStockName.font = [UIFont systemFontOfSize:16];
    _pStockCode.font = [UIFont fontWithName:@"STHeitiSC-Light" size:14];
    _pStockName.textColor = kMYHexToRGB(0x333333, 1);
    _pStockCode.textColor = kMYHexToRGB(0x333333, 1);
    _pImageView.contentMode = UIViewContentModeCenter;
    
//    _pClearButton.titleLabel.font = KSSStockView_searchList_font;
//    [_pClearButton setTitleColor:KStockSearchHexToRGB(0x888888, 1) forState:UIControlStateNormal];
    self.contentView.backgroundColor = kMYHexToRGB(0xf5f5f5, 1);
//    _pClearButton.backgroundColor = KStockSearchHexToRGB(0xefefef, 1);
    [_pAddZiXuanBtn setImage:[UIImage imageNamed:@"search_addstock"] forState:UIControlStateNormal];
    [_pAddZiXuanBtn setImage:[UIImage imageNamed:@"search_addsuccess"] forState:UIControlStateSelected];
    [_pAddZiXuanBtn addTarget:self action:@selector(addZiXuanButton:) forControlEvents:UIControlEventTouchUpInside];
//    [_pClearButton setImage:[UIImage imageNamed:@"remove_unselected"] forState:UIControlStateNormal];
    self.backgroundColor = kMYHexToRGB(0xf5f5f5, 1);
    
}

- (void)initView:(CGFloat)cellHight cellModel:(MYSearchResultModel *)cellModel indexPath:(NSIndexPath *)indexPath cellW:(CGFloat)cellW
{
    [self cleaOldData];
    
    [self updateNewData:cellModel];
    [self updateFrame:cellHight cellW:cellW];
    
    _pAddZiXuanBtn.tag = indexPath.row;//绑定tag
}

- (void)updateNewData:(MYSearchResultModel *)cellModel
{
//    _pStockName.text = cellModel.m_stockName;
    _pStockCode.text = [NSString stringWithFormat:@"%@   %@", cellModel.m_stockCode, cellModel.m_stockName];
    
    if (cellModel.m_isCreated) {
        [_pAddZiXuanBtn setImage:[UIImage imageNamed:@"create"] forState:UIControlStateNormal];
    }else {
        _pAddZiXuanBtn.selected = cellModel.m_isSelected;
    }
    
    _pImageView.hidden = NO;
    if (cellModel.n_TradeId == 1) {
        _pImageView.image = [UIImage imageNamed:@"sh"];
    }else if (cellModel.n_TradeId == 2) {
        _pImageView.image = [UIImage imageNamed:@"sz"];
    }else if (cellModel.n_TradeId == 24 || cellModel.n_TradeId == 6) {
        _pImageView.image = [UIImage imageNamed:@"xg"];
    }else {
        _pImageView.hidden = YES;
    }
    
//    if (cellModel.m_searchType == KMSSearch_history_Type) {
//        _pClearButton.hidden = NO;
//        _pStockName.hidden = _pStockCode.hidden = _pAddZiXuanBtn.hidden = YES;
//    }else{
//        _pStockName.hidden = _pStockCode.hidden = _pAddZiXuanBtn.hidden = NO;
//    }
    
}

- (void)updateFrame:(CGFloat)cellHight cellW:(CGFloat)cellW
{
    if (cellW > 0) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, cellW, cellHight);
    }
    
    CGFloat fItemW = (CGRectGetWidth(self.frame) - 15 - 10 - cellHight)/2;
    
    _pImageView.frame = CGRectMake(15, 0, 30, cellHight);
//    _pStockName.frame = CGRectMake(15, 0, fItemW, cellHight);
    _pStockCode.frame = CGRectMake(CGRectGetMaxX(_pImageView.frame) + 20, 0, fItemW*2, cellHight);
    _pAddZiXuanBtn.frame = CGRectMake(self.frame.size.width - cellHight - 10, 0, cellHight, cellHight);
}

- (void)cleaOldData
{
    [_pStockName setText:@""];
    [_pStockCode setText:@""];
}

- (void)addClickOnButtonBlockBack:(ClickOnButtonBack)clickOnButtonBlockBack
{
    self.BlockClickOnButtonBack = clickOnButtonBlockBack;
}

- (void)addZiXuanButton:(UIButton *)btn
{
    _BlockClickOnButtonBack(btn);
}

- (void)setIsHideAddButton:(BOOL)isHideAddButton
{
    _isHideAddButton = isHideAddButton;
    self.pAddZiXuanBtn.hidden = _isHideAddButton;
}

@end
